<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入类型名称"></el-input>
      </el-form-item>
      <el-form-item label="展示图片" prop="picUrl">
        <single-image-upload prcScale="800x600" module="picUrl" v-model="dataForm.picUrl"></single-image-upload>
      </el-form-item>
      <el-form-item label="排序" prop="orderNumber">
        <el-input v-model="dataForm.orderNumber" placeholder="请输入排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="dataFormSubmit()">确定</el-button>
      <el-button size="small" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleImageUpload from '@/components/UploadImage/SingleImageUpload';
import { getFirstaidTypeInfo, createFirstaidType, updateFirstaidType } from '@/api/knowledge/firstaidtype';

const defaultDataForm = {
  id: '',
  name: '',
  picUrl: '',
  orderNumber: '',
}

export default {
  components: {
    SingleImageUpload,
  },
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        name: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' }
        ],

        picUrl: [
          { required: true, message: '显示图片不能为空', trigger: 'blur' }
        ],

        orderNumber: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],

      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getFirstaidTypeInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.name = res.data.name;
              this.dataForm.picUrl = res.data.picUrl;
              this.dataForm.orderNumber = res.data.orderNumber;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            name: this.dataForm.name,
            picUrl: this.dataForm.picUrl,
            orderNumber: this.dataForm.orderNumber,
          }
          if (!this.dataForm.id) {
            // 创建
            createFirstaidType(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // 更新
            updateFirstaidType(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
